﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FormCommand.cs" company="Marcelo Roca">
//   Copyright (c) 2011 Marcelo Roca. All rights reserved.
// </copyright>
// <author name="Marcelo Roca" email="marce@marceloroca.com"/>
// <license>
//   This program is free software: you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
// 
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program.  If not, see http://www.gnu.org/licenses/.
// </license>
// --------------------------------------------------------------------------------------------------------------------

using System;

namespace GameKits.Unity.GuiForms.Forms
{
    public abstract class FormCommand
    {
        /// <summary>
        /// Indica si la instancia fue inicializada
        /// </summary>
        private bool initialized;

        public event EventHandler Executed;

        /// <summary>
        /// Gets Parent.
        /// </summary>
        protected Form Parent
        {
            get;
            private set;
        }

        /// <summary>
        /// Ejecuta el comando
        /// </summary>
        /// <returns>
        /// Retorna true si terminó
        /// </returns>
        public bool Execute()
        {
            if (!this.initialized)
            {
                this.Initialize();
                this.initialized = true;
            }

            if (this.DoExecute())
            {
                if (this.Executed != null)
                {
                    this.Executed(this.Parent, new EventArgs());
                }

                return true;
            }

            return false;
        }


        /// <summary>
        /// Colocamos el parent al comando
        /// </summary>
        /// <param name="parent">
        /// The parent.
        /// </param>
        /// <exception cref="AccessViolationException">
        /// </exception>
        internal void SetParent(Form parent)
        {
            if (this.Parent == null)
            {
                this.Parent = parent;
            }
            else
            {
                throw new AccessViolationException("No se puede reescribir el parent");
            }
        }

        /// <summary>
        /// Inicializa la instancia
        /// </summary>
        protected virtual void Initialize()
        {
        }

        /// <summary>
        /// Ejecuta el comando
        /// </summary>
        /// <returns>
        /// Devolver true para indicar que ya se ejecutó (y no se volverá a ejecutar
        /// </returns>
        protected abstract bool DoExecute();
    }
}
